<template>
  <div>
    <el-dialog
      :title="schoolId ? '编辑学校' : '新增学校'"
      :close-on-click-modal="false"
      :visible.sync="visible"
      width="700px"
    >
      <el-form
        :model="schoolForm"
        :rules="schoolFormRules"
        ref="schoolForm"
        label-width="100px"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="学校名称：" prop="schoolName">
              <el-input
                v-model="schoolForm.schoolName"
                placeholder="请输入学校名称"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属地区：" prop="areaAll">
              <el-cascader
                v-if="$store.state.common.regions[0]"
                v-model="schoolForm.areaAll"
                :options="$store.state.common.regions"
                @change="handleChange"
                placeholder="请选择所属地区"
                clearable
              ></el-cascader>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="学校性质：" prop="schoolType">
              <nice-select
                v-model="schoolForm.schoolType"
                placeholder="请选择学校性质"
                clearable
                :options="options.schoolType"
                labelkey="name"
                valuekey="id"
              ></nice-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="覆盖年级：" prop="coverageGrade">
              <nice-select
                v-model="schoolForm.coverageGrade"
                placeholder="请选择覆盖年级"
                clearable
                :multiple="true"
                :options="options.coverageGrade"
              ></nice-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="学生规模：" prop="studentScale">
              <nice-select
                v-model="schoolForm.studentScale"
                placeholder="请选择学生规模"
                clearable
                :options="options.studentScale"
                labelkey="name"
                valuekey="id"
              ></nice-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="校长姓名：" prop="schoolMaster">
              <el-input
                v-model="schoolForm.schoolMaster"
                placeholder="请输入校长姓名"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false" size="small">取消</el-button>
        <el-button
          type="primary"
          @click="dataFormSubmit"
          size="small"
          v-preventReClick
          >确定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { addSchool, adminschoolupdate } from '@/api/user'
export default {
  data () {
    return {
      visible: false,
      schoolForm: {
        schoolName: '',
        areaAll: [],
        schoolType: '',
        coverageGrade: [],
        studentScale: '',
        schoolMaster: ''
      },
      schoolId: '',
      schoolFormRules: {
        schoolName: [
          { required: true, message: '学校名称不能为空', trigger: 'blur' }
        ],
        areaAll: [
          { required: true, message: '请选择所属地区', trigger: 'change' }
        ]
      }
    }
  },
  props: {
    options: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  methods: {
    init (obj) {
      this.visible = true

      if (obj) {
        this.schoolForm = obj
        this.schoolId = obj.id
      } else {
        this.schoolForm = {
          schoolName: '',
          areaAll: [],
          schoolType: '',
          coverageGrade: [],
          studentScale: '',
          schoolMaster: ''
        }
        this.schoolId = ''
      }
      this.$nextTick(() => {
        this.$refs['schoolForm'].resetFields()
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['schoolForm'].validate(valid => {
        if (valid) {
          let nurseryGrade = 0,
            juniorgrade = 0,
            middleGrade = 0,
            highGrade = 0
          this.options.coverageGrade.forEach(item => {
            this.schoolForm.coverageGrade.forEach(ele => {
              if (ele === item.value) {
                if (item.label === '幼儿园') {
                  nurseryGrade = 1
                } else if (item.label === '小学') {
                  juniorgrade = 1
                } else if (item.label === '初中') {
                  middleGrade = 1
                } else if (item.label === '高中') {
                  highGrade = 1
                }
              }
            })
          })
          this.submit(nurseryGrade, juniorgrade, middleGrade, highGrade)
        }
      })
    },
    async submit (nurseryGrade, juniorGrade, middleGrade, highGrade) {
      let regions = this.$filterRegion(
        {
          province: this.schoolForm.areaAll[0],
          city: this.schoolForm.areaAll[1],
          area: this.schoolForm.areaAll[2]
        },
        this.$store.state.common.regions
      )
      let _api = this.schoolId ? adminschoolupdate : addSchool
      let res = await _api({
        ...this.schoolForm,
        coverageGrade: 0,
        address: '',
        area: this.schoolForm.areaAll[2],
        areaName: regions[2],
        city: this.schoolForm.areaAll[1],
        cityName: regions[1],
        province: this.schoolForm.areaAll[0],
        provinceName: regions[0],
        highGrade: highGrade,
        juniorGrade: juniorGrade,
        level: '',
        middleGrade: middleGrade,
        nurseryGrade: nurseryGrade,
        openStatus: 1,
        schoolMaster: this.schoolForm.schoolMaster,
        schoolName: this.schoolForm.schoolName,
        schoolType: this.schoolForm.schoolType,
        studentScale: this.schoolForm.studentScale,
        teacherScale: ''
      })
      if (res[0]) {
        return
      }
      let data = res[1]
      if (data && data.code === 0) {
        this.$message({
          message: '操作成功',
          type: 'success',
          duration: 1500,
          onClose: () => {
            this.visible = false
            this.$emit('refreshDataList')
          }
        })
      } else {
        this.$message.error(data.msg)
      }
    },
    handleChange () {}
  }
}
</script>
